﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections.Generic;

public partial class System_Controls_Common_FileManagerTreeView : System.Web.UI.UserControl
{
    #region Properties
    //==========================================================================
    private string _RootPath;
    public string RootPath
    {
        get { return _RootPath; }
        set { _RootPath = value; }
    }
    //==========================================================================
    private bool _ShowFiles;
    public bool ShowFiles
    {
        get { return _ShowFiles; }
        set { _ShowFiles = value; }
    }
    //==========================================================================
    private string _FileFilter = "";
    public string FileFilter
    {
        get { return _FileFilter; }
        set { _FileFilter = value; }
    }
    //==========================================================================
    private string _SelectedVirtualPath = "";
    public string SelectedVirtualPath
    {
        get 
        {
            if (FileManager_TreeView.SelectedNode == null)
            {
                return _SelectedVirtualPath;
            }
            else 
            { 
                return FileManager_TreeView.SelectedNode.ValuePath;
            }
        }
        set { _SelectedVirtualPath = value; }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            lb_SelectedVirtualPath.Text = "";
            TreeNode rootNode = new TreeNode(_RootPath, _RootPath, "~/System/Design/ico/Common/folder.gif");
            rootNode.Expanded = true;
            FileManager_TreeView.Nodes.Add(rootNode);
        }
    }
    private void BindTreeToDirectory(string dirPath, TreeNode parentNode)
    {
        DataTable myTable = SiteFox.Business.Logic.FileManager.GetAllContent(dirPath);
        for (int i = 0; i < myTable.Rows.Count; i++)
        {
            string myNodeIcon = myTable.Rows[i].ItemArray[1].ToString();
            string myNodeTitle = myTable.Rows[i].ItemArray[2].ToString();
            string myNodeExt = myTable.Rows[i].ItemArray[4].ToString();
            string myNodeType = myTable.Rows[i].ItemArray[5].ToString();
            TreeNode node = new TreeNode(myNodeTitle, myNodeTitle, myNodeIcon);
            if (myNodeType == SiteFox.Business.Logic.FileManager.ObjectType.DirectoryInfo.ToString())
            {
                parentNode.ChildNodes.Add(node);
            }
            else if (myNodeType == SiteFox.Business.Logic.FileManager.ObjectType.FileInfo.ToString() && _ShowFiles)
            {
                if (_FileFilter == "")
                {
                    parentNode.ChildNodes.Add(node);
                }
                else if (_FileFilter == myNodeExt)
                {
                    parentNode.ChildNodes.Add(node);
                }
            }
        }
        parentNode.Expand();
    }
    protected void FileManager_TreeView_SelectedNodeChanged(object sender, EventArgs e)
    {
        lb_SelectedVirtualPath.Text = FileManager_TreeView.SelectedNode.ValuePath.ToString();
        TreeNode node1 = FileManager_TreeView.SelectedNode;
        node1.ChildNodes.Clear();
        BindTreeToDirectory(lb_SelectedVirtualPath.Text, node1);
    }
}